package cn.com.dhcc.ddi.stat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.stat.dao.DataSyncReceiveStatDao;
import cn.com.dhcc.ddi.stat.vo.DataSyncStatVo;
/**
 * 
    * @ClassName: DataSyncRecStatService
    * @Description: 接收数据统计service
    * @author 18315
    * @date 2016-12-26
    *
 */
@Service
public class DataSyncRecStatService extends BaseService {
	
	@Autowired
	private DataSyncReceiveStatDao receiveStatDao;
	
	/**
	 * 获取所有节点数据量统计数据（按天，月或者年）
	 * @param scope 0-天，1-月，2-年
	 * @param date 日期
	 * @return
	 * @throws ServiceException
	 */
	public Map<String, String> getRecDatasCount(int scope, String date) throws ServiceException {
		Map<String, String> data = new HashMap<String, String>();
		
		switch(scope) {
		case 0:
			data = getRecDatasCountByDay(date);
			break;
			
		case 1:
			data = getRecDatasCountByMonth(date);
			break;
			
		case 2:
			data = getRecDatasCountByYear(date);
			break;
			
		default:
			data = getRecDatasCountByDay(date);
			break;
		}
		
		return data;
		
		
	}
	
	/**
	 * 按天获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecDatasCountByDay(String date) throws ServiceException {
		try {
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getNum());
				data.put(vo.getCataId(), "" + vo.getNum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按月获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecDatasCountByMonth(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01";
			String end = date + "-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getNum());
				data.put(vo.getCataId(), "" + vo.getNum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部节点", e);
		}
	}
	
	/**
	 * 按年获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecDatasCountByYear(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01-01";
			String end = date + "-12-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getNum());
				data.put(vo.getCataId(), "" + vo.getNum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部节点", e);
		}
	}
	
	/**
	 * 获取所有节点频次统计数据（按天，月或者年）
	 * @param scope 0-天，1-月，2-年
	 * @param date 日期
	 * @return
	 * @throws ServiceException
	 */
	public Map<String, String> getRecTotalCount(int scope, String date) throws ServiceException {
		Map<String, String> data = new HashMap<String, String>();
		
		switch(scope) {
		case 0:
			data = getRecTotalCountByDay(date);
			break;
			
		case 1:
			data = getRecTotalCountByMonth(date);
			break;
			
		case 2:
			data = getRecTotalCountByYear(date);
			break;
			
		default:
			data = getRecTotalCountByDay(date);
			break;
		}
		
		return data;
	}
	
	/**
	 * 按天获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecTotalCountByDay(String date) throws ServiceException {
		try {
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getSum());
				data.put(vo.getCataId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按月获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecTotalCountByMonth(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01";
			String end = date + "-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getSum());
				data.put(vo.getCataId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部节点", e);
		}
	}
	
	/**
	 * 按年获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getRecTotalCountByYear(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01-01";
			String end = date + "-12-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<DataSyncStatVo> recVos = receiveStatDao.getReceiveInfo(start, end);
			
			for(DataSyncStatVo vo: recVos) {
				data.put(vo.getNodeId(), "" + vo.getSum());
				data.put(vo.getCataId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部节点", e);
		}
	}
	
	/**
	 * 更新
	 */
	public int insert(DataSyncStatVo vo) throws ServiceException {
			
			try {
				return receiveStatDao.insert(vo);
			} catch (DaoException e)  {
				throw new ServiceException("新增失败", e);
			}
		}
	  public int update(DataSyncStatVo vo) throws ServiceException {
			
			try {
				return receiveStatDao.update(vo);
			}catch (DaoException e)  {
				throw new ServiceException("更新失败", e);
			}
		}

}
